package com.unbox.admin.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unbox.admin.entity.UnboxUser;
import com.unbox.admin.entity.UserRoles;
import com.unbox.admin.mapper.UnboxUserMapper;
import com.unbox.admin.service.IUnboxUserService;
import com.unbox.admin.service.IUserRolesService;
import com.unbox.common.table.UnBoxPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-01-26
 */
@Service
public class UnboxUserServiceImpl extends ServiceImpl<UnboxUserMapper, UnboxUser> implements IUnboxUserService {

    @Autowired
    IUserRolesService rolesService;

    @Override
    public UnboxUser get(UnboxUser unboxUser) {
        LambdaQueryWrapper<UnboxUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(unboxUser.getId())) {
            lambdaQueryWrapper.eq(UnboxUser::getId, unboxUser.getId());
        }
        if (!StringUtils.isEmpty(unboxUser.getUserName())) {
            lambdaQueryWrapper.eq(UnboxUser::getUserName, unboxUser.getUserName());
        }
        return super.getOne(lambdaQueryWrapper);
    }

    @Override
    public List<UnboxUser> getList(UnboxUser unboxUser) {
        LambdaQueryWrapper<UnboxUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        return super.list(lambdaQueryWrapper);
    }

    @Override
    public UnBoxPage<List<UnboxUser>> getPageList(UnboxUser unboxUser, int pageNo, int pageSize) {
        LambdaQueryWrapper<UnboxUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(unboxUser.getName())) {
            lambdaQueryWrapper.like(UnboxUser::getName, unboxUser.getName());
        }
        if (!StringUtils.isEmpty(unboxUser.getUserName())) {
            lambdaQueryWrapper.like(UnboxUser::getUserName, unboxUser.getUserName());
        }
        lambdaQueryWrapper.orderByDesc(UnboxUser::getCreateTime);
        Page<UnboxUser> page = new Page<>(pageNo,pageSize);
        IPage<UnboxUser> pageResult = this.baseMapper.selectPage(page, lambdaQueryWrapper);
        UnBoxPage<List<UnboxUser>> unBoxPage = new UnBoxPage<>();
        unBoxPage.setPageNo(pageNo);
        unBoxPage.setPageSize(pageSize);
        unBoxPage.setSumPageCount(pageResult.getPages());
        unBoxPage.setSumDataCount(pageResult.getTotal());
        unBoxPage.setDatas(pageResult.getRecords());
        return unBoxPage;
    }

    @Override
    public List<UnboxUser> getIdsList(List<Integer> ids) {
        LambdaQueryWrapper<UnboxUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(UnboxUser::getId, ids);
        return super.list(lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean save(UnboxUser entity) {
        super.save(entity);
        List<UserRoles> roles = entity.getRoleList().stream()
                .map(role -> {
                    UserRoles userRoles = new UserRoles();
                    userRoles.setUserId(entity.getId());
                    userRoles.setRoleId(role.getId());
                    return userRoles;
                }).collect(Collectors.toList());
       return rolesService.saveBatch(roles);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean saveBatch(List<UnboxUser> entityList) {
        return super.saveBatch(entityList);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean update(UnboxUser unboxUser) {
        LambdaQueryWrapper<UnboxUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UnboxUser::getId, unboxUser.getId());

        // 保存新角色
        if (unboxUser.getRoleList() != null) {
            // 删除角色
            LambdaQueryWrapper<UserRoles> userRolesLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userRolesLambdaQueryWrapper.eq(UserRoles::getUserId, unboxUser.getId());
            rolesService.remove(userRolesLambdaQueryWrapper);
            List<UserRoles> roles = unboxUser.getRoleList().stream()
                    .map(role -> {
                        UserRoles userRoles = new UserRoles();
                        userRoles.setUserId(unboxUser.getId());
                        userRoles.setRoleId(role.getId());
                        return userRoles;
                    }).collect(Collectors.toList());
            rolesService.saveBatch(roles);
        }
        return super.update(unboxUser, lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateBatchById(List<UnboxUser> entityList) {
        return super.updateBatchById(entityList);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean remove(UnboxUser unboxUser) {
        LambdaQueryWrapper<UnboxUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UnboxUser::getId, unboxUser.getId());
        LambdaQueryWrapper<UserRoles> userRolesLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userRolesLambdaQueryWrapper.eq(UserRoles::getUserId, unboxUser.getId());
        rolesService.remove(userRolesLambdaQueryWrapper);
        return super.remove(lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean removeByIds(List<Integer> idList) {
        LambdaQueryWrapper<UserRoles> userRolesLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userRolesLambdaQueryWrapper.in(UserRoles::getUserId, idList);
        rolesService.remove(userRolesLambdaQueryWrapper);
        return super.removeByIds(idList);
    }
}
